﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Code.BLL
{
    public class RegionProvider
    {
        #region Quản lý Region

        public static void UIRegion(Region theRegion, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IURegion";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@Description", theRegion.Description));
            command.Parameters.Add(new SqlParameter("@RegionCode", theRegion.RegionCode));
            command.Parameters.Add(new SqlParameter("@RegionID", theRegion.RegionID));
            command.Parameters.Add(new SqlParameter("@RegionName", theRegion.RegionName));
            command.Parameters.Add(new SqlParameter("@ServiceCost", theRegion.ServiceCost));
            command.Parameters.Add(new SqlParameter("@DeletedFlag", theRegion.DeletedFlag));
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void DeleteRegionByRegionID(Int32 regionID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteRegionByRegionID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@RegionID", regionID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static Region GetRegionByRegionID(Int32 regionID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetRegionByRegionID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@RegionID", regionID));
            DataSet ds = DataProvider.ExecuteDataSet(command);
            Region theRegion = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theRegion = new Region();
                theRegion.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theRegion.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                theRegion.RegionCode = ds.Tables[0].Rows[0]["RegionCode"].ToString();
                theRegion.RegionID = Int32.Parse(ds.Tables[0].Rows[0]["RegionID"].ToString());
                theRegion.RegionName = ds.Tables[0].Rows[0]["RegionName"].ToString();
                theRegion.ServiceCost = float.Parse(ds.Tables[0].Rows[0]["ServiceCost"].ToString());
            }

            return theRegion;
        }

        public static DataSet GetRegions()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetRegions";
            command.CommandType = CommandType.StoredProcedure;
            DataSet ds = DataProvider.ExecuteDataSet(command);
            return ds;
        }

        public static DataSet GetRegionsWithPaging(Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetRegionsWithPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }


        #endregion
    }
}